<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue'
import { ArrowRight } from '@element-plus/icons-vue'
import FlexPlaylist from '@/components/common/FlexPlaylist.vue'

import { useRecommendHook } from '@/store/modules/recommend'

const recommendStore = useRecommendHook()

onMounted(() => {
  getPlaylist()
})
const getPlaylist = () => {
  recommendStore.initPlayList(
    'hot',
    100,
    (currentFilters.value.offset - 1) * 100,
    currentFilters.value.cat
  )
}
const hotPlaylist = computed(() => recommendStore.hotPlaylist)
const hotCatlist = computed(() => recommendStore.hotCatlist)
const catlist = computed(() => {
  const icons = [
    'icon-yuzhongguanli1',
    'icon-fengge',
    'icon-kafei',
    'icon-kaixin',
    'icon-zhuti'
  ]
  const cats = []
  for (let key in recommendStore.cats.categories) {
    cats.push({
      category: recommendStore.cats.categories[key],
      catlist: [],
      icon: icons[key]
    })
  }
  recommendStore.cats.sub.forEach((catItem) => {
    cats[catItem.category].catlist.push(catItem)
  })

  return cats
})
const currentFilters = ref({
  offset: 1,
  cat: ''
})

const selectCat = (cat: string) => {
  currentFilters.value.cat = cat
  getPlaylist()
}
</script>

<template>
  <div class="playlist_container">
    <div class="playlist_tags">
      <el-popover
        placement="bottom-start"
        trigger="click"
        popper-class="cats_container"
      >
        <template #reference>
          <div class="cat_button">
            {{ currentFilters?.cat ? currentFilters.cat : '全部歌单'
            }}<el-icon><ArrowRight /></el-icon>
          </div>
        </template>
        <template v-for="(catItem, index) in catlist" :key="index">
          <div class="cat_item">
            <div class="cat_item__category">
              <i :class="catItem.icon" class="cat_icon" />
              {{ catItem.category }}
            </div>
            <div class="cat_item__content">
              <template v-for="cat in catItem.catlist" :key="cat.name">
                <div
                  class="cat"
                  :class="{ 'is-active': currentFilters.cat === cat.name }"
                  @click="selectCat(cat.name)"
                >
                  <span class="cat_name">
                    {{ cat.name }}
                    <i class="cat_hot" v-if="cat.hot">hot</i>
                  </span>
                </div>
              </template>
            </div>
          </div>
        </template>
      </el-popover>
      <div>
        <template v-for="cat in hotCatlist" :key="cat.id">
          <span
            class="tag_item"
            :class="{ 'is-active': currentFilters.cat === cat.name }"
            @click="selectCat(cat.name)"
            >{{ cat.name }}</span
          >
        </template>
      </div>
    </div>

    <FlexPlaylist :playlist="hotPlaylist" />

    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="1000"
        :default-page-size="100"
        v-model:current-page="currentFilters.offset"
        @current-change="getPlaylist"
      />
    </div>
  </div>
</template>

<style scoped lang="scss">
.playlist_container {
  padding: 20px 0 40px;
  .playlist_tags {
    height: 42px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }
  .tag_item {
    font-size: 14px;
    color: var(--music-link-color);
    padding: 4px 10px;
    border-radius: 30px;
    cursor: pointer;
    &:hover {
      color: var(--music-link-hover-color);
    }
  }
  .tag_item.is-active {
    color: var(--music-base-color);
    background-color: var(--music-base-color-light);
  }
}

.cat_button {
  width: 120px;
  height: 32px;
  line-height: 32px;
  text-align: center;
  border: 1px solid var(--music-color-5);
  border-radius: 32px;
  color: var(--music-recommend-text-color);
  .el-icon {
    vertical-align: text-top;
  }
  &:hover {
    background-color: var(--music-color-1);
  }
}

.cat_item {
  display: flex;
  line-height: 40px;
  margin-bottom: 30px;
  font-size: 16px;
  &__category {
    width: 100px;
    color: var(--music-color-7);
  }
  &__content {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
  }
  .cat_icon {
    font-size: 32px;
    vertical-align: sub;
    padding: 0 4px;
    color: var(--music-link-color);
  }
  .cat {
    width: 15%;
    padding-left: 30px;
    color: var(--music-link-color);
    cursor: pointer;
    &:hover {
      color: var(--music-link-hover-color);
    }
    .cat_name {
      position: relative;
    }
    .cat_hot {
      position: absolute;
      font-size: 12px;
      top: -2px;
      right: -22px;
      color: var(--music-base-color);
    }
  }
  .cat.is-active {
    color: var(--music-base-color);
  }
}

.pagination {
  width: 100%;
  display: flex;
  justify-content: center;
}
</style>

<style lang="scss">
.cats_container {
  width: 50% !important;
  overflow: hidden;
  padding: 20px !important;
}
</style>
